<template>
     <div class="components-container">
         <div class="header-form">
            <div class="layui-inline">
                <label class="layui-form-label">收费状态</label>
                <div class="layui-inline">
                  <el-radio-group v-model="selectData.ChdstatusEquals" @change="getData">
                      <el-radio-button :label="all">全部</el-radio-button>
                    <el-radio-button v-for="item in chargetype" :key="item.Value" :label="item.Value">{{item.Text}}</el-radio-button>
                  </el-radio-group>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" >业务编号</label>
                <div class="layui-input-inline">
                    <el-input v-model="selectData.RegbusnumEquals" @keydown.enter.native="btnSearch"></el-input>
                </div>
            </div>
            <div class="layui-inline" v-if="!istime">
                  <label class="layui-form-label" >处理日期</label>
                  <el-date-picker v-date v-model="selectData.ChddateGreaterAndEquals" type="date" placeholder="大于等于该日期" @change="btnSearch">
                  </el-date-picker>
                  <el-date-picker v-date v-model="selectData.ChddateLessAndEquals" type="date" placeholder="小于等于该日期" @change="btnSearch">
                  </el-date-picker>
            </div>
            <div class="layui-inline" v-if="istime">
                  <label class="layui-form-label" >处理日期</label>
                  <!-- <el-date-picker v-date v-model="selectData.ChddateGreaterAndEquals" type="date" placeholder="大于等于该日期" >
                  </el-date-picker>
                  <el-date-picker v-date v-model="selectData.ChddateLessAndEquals" type="date" placeholder="小于等于该日期" >
                  </el-date-picker> -->
                  <el-date-picker v-model="selectData.ChddateGreaterAndEquals" type="datetime" placeholder="选择日期时间" default-time="12:00:00" @change="btnSearch"></el-date-picker>
                  <el-date-picker v-model="selectData.ChddateLessAndEquals" type="datetime" placeholder="选择日期时间" default-time="12:00:00" @change="btnSearch"></el-date-picker>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">逝者姓名</label>
              <div class="layui-inline">
                <el-input v-model="selectData.RegnameLike" placeholder="请输入逝者姓名" @keydown.enter.native="btnSearch"></el-input>
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">客户姓名</label>
              <div class="layui-inline">
                <el-input v-model="selectData.ReghandnameLike" placeholder="请输入客户姓名" @keydown.enter.native="btnSearch"></el-input>
              </div>
            </div>
            <el-button type="primary" :disabled="downloadLoading" @click="btnSearch" :loading="this.$store.state.isloading">搜索</el-button>
            <el-button :disabled="this.$store.state.isloading || downloadLoading"  @click="btnRepeat">重置</el-button>
        </div>
        <div>
          <ul class="toolList">
            <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
              <el-button type="primary" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
            </li>
            <li>
              <!-- style="margin-left:20px;margin-bottom:-10px;" -->
            <el-button type="primary" :disabled="this.$store.state.isloading" @click="exportExcel">导出Excel</el-button>
            <el-button type="primary" :disabled="this.$store.state.isloading || downloadLoading" v-if="btnShow('结账')" @click="checkouts">结账</el-button>
            </li>
          </ul>
        </div>
        <dataTable :headList="headList" :dataList="dataList" :pageOrder="pageOrder" :hiddenPageOrder="false" :height="height" @pageChange="pageChange" @selectRow="selectRow"></dataTable>
        <checkoutinfo ref="simpleReg" @refreshData="getData"></checkoutinfo>
        <printmodal ref="printPdf"></printmodal>
     </div>
</template>

<script>
import dataTable from "@/components/Table/DataTable";
import apiBase from "@/api/apiBase.js";
import checkoutinfo from "../businessHall/checkoutAddfh";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import printmodal from "@/components/Pdf/Print";
import { PRINT } from "@/components/Pdf/PDF.js";
import { manage } from "@/mixins/managePage.js";
import { ORDER } from "@/const/orderConst.js";
const INFO = "详情";
const EXPORT = "导出";
const DELETE = "作废";
export default {
  name: "businesschargeSearch",
  mixins: [manage],
  data() {
    return {
      all: null,
      selectData: {
        ChdstatusEquals: null,
        RegbusnumEquals: null,
        ChddateGreaterAndEquals: null,
        ChddateLessAndEquals: null
      },
      chdData: {},
      chargetype: [],
      pageWidth: 700,
      maxHeight: 500,
      title: null,
      order: null,
      height: "620",
      downloadLoading:false,
      istime: null,
      istoday:null,
    };
  },
  methods: {
    exportExcel() {
      var startTime;
      var endTime;
      if(this.selectData.ChddateGreaterAndEquals){
        startTime = this.selectData.ChddateGreaterAndEquals.toLocaleDateString();
      }else{
        startTime = '空';
      }
      if(this.selectData.ChddateLessAndEquals){
        endTime = this.selectData.ChddateLessAndEquals.toLocaleDateString();
      }else{
        endTime = '空';
      }
      let useState;
      if(this.selectData.ChdstatusEquals == null){
        useState = '业务收费查询' + '_' + '全部' + '(' + startTime + '--' + endTime + ')';
      }else if(this.selectData.ChdstatusEquals == 'A'){
        useState = '业务收费查询' + '_' + '未结账' + '(' + startTime + '--' + endTime + ')';
      }else if(this.selectData.ChdstatusEquals == 'B'){
        useState = '业务收费查询' + '_' + '已结账' + '(' + startTime + '--' + endTime + ')';
      }else if(this.selectData.ChdstatusEquals == 'C'){
        useState = '业务收费查询' + '_' + '已作废' + '(' + startTime + '--' + endTime + ')';
      }else if(this.selectData.ChdstatusEquals == 'D'){
        useState = '业务收费查询' + '_' + '已挂账' + '(' + startTime + '--' + endTime + ')';
      }else if(this.selectData.ChdstatusEquals == 'E'){
        useState = '业务收费查询' + '_' + '坏账' + '(' + startTime + '--' + endTime + ')';
      }
       if (this.dataList.length === 0){
          this.$message.error("无导出数据！");
          return;
      }
      this.downloadLoading = true;
      require.ensure([], () => {
        const { export_json_to_excel } = require("@/vendor/Export2Excel");
        export_json_to_excel(this.headList, this.dataList, useState);
        this.downloadLoading = false;
      });
    },
    //功能操作
    operate(order) {
      this.order = order;
      switch (this.order) {
        case INFO:
          this.$refs.simpleReg.open(this.order, this.currentRow.CHDID);
          break;
        case EXPORT:
          this.$refs.printPdf.report(PRINT.PDFBUSINESSCHARGESEARCH,this.getDataC());
          break;
        case DELETE:
          this.chdInvalid();
          break;
        case '取消结账':
          this.chdCancel();
          break;
        case '取消挂账':
          this.CancelBill();
          break;
        default:
      }
    },
    //取消挂账
    CancelBill() {
      this.$confirm("确认是否取消挂账?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(callback => {
          let data = {
            CHDID: this.currentRow.CHDID
          };
          apiBase.DoService(
            data,
            apiBase.getApiAdress().Checkout.CancelBill,
            res => {
              this.$message({
                type: "success",
                message: "取消挂账成功!"
              });
              this.getData();
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    //取消结账
    chdCancel() {
      this.$confirm("确认是否取消结账?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(callback => {
          let data = {
            CHDID: this.currentRow.CHDID
          };
          apiBase.DoService(
            data,
            apiBase.getApiAdress().Checkout.CancelCheckout,
            res => {
              this.$message({
                type: "success",
                message: "取消结账成功!"
              });
              this.getData();
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    checkouts(){
      this.$refs.simpleReg.open(ORDER.CHECKOUT, this.currentRow.CHDID);//结账
    },
    getColumns() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Column.GetBusschargeColumns,
        res => {
          this.headList = res.data.content;
        }
      );
    },
    getDataC(){
      let Data = JSON.parse(JSON.stringify(this.selectData));
      if (!this.selectData.RegbusnumEquals) {
        Data.RegbusnumEquals = null;
      }
      Data.ChddateGreaterAndEquals = formatDate(
        Data.ChddateGreaterAndEquals,
        "yyyyMMdd"
      );
      Data.ChddateLessAndEquals = formatDate(
        Data.ChddateLessAndEquals,
        "yyyyMMdd"
      );
      if(this.istime){
        Data.ChdtimeGreaterAndEquals = formatDate(
          this.selectData.ChddateGreaterAndEquals,
          "hhmmss"
        );
        Data.ChdtimeLessAndEquals = formatDate(
          this.selectData.ChddateLessAndEquals,
          "hhmmss"
        );
      };
      let data = {
        PageIndex: 1,
        PageSize: 1000,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };
      return data;
    },
    getData() {
      let Data = JSON.parse(JSON.stringify(this.selectData));
      if (!this.selectData.RegbusnumEquals) {
        Data.RegbusnumEquals = null;
      }
      Data.ChddateGreaterAndEquals = formatDate(
        Data.ChddateGreaterAndEquals,
        "yyyyMMdd"
      );
      Data.ChddateLessAndEquals = formatDate(
        Data.ChddateLessAndEquals,
        "yyyyMMdd"
      );
      if(this.istime){
        Data.ChdtimeGreaterAndEquals = formatDate(
          this.selectData.ChddateGreaterAndEquals,
          "hhmmss"
        );
        Data.ChdtimeLessAndEquals = formatDate(
          this.selectData.ChddateLessAndEquals,
          "hhmmss"
        );
      };
      let data = {
        PageIndex: 1,
        PageSize: 1000,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Checkout.QueryBusscharge,
        res => {
          this.dataList = res.data.content.OutData;
          this.pageOrder.totalNum = res.data.content.RecordCount;
        }
      );
    },
    //获取业务状态
    getChdStatus() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Checkout.GetChdStatus,
        res => {
          this.chargetype = res.data.content;
        }
      );
    },
     gettoday() {
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Param.GetIsBusinesschargeSearchDefaultToday,
        res => {
          this.istoday = res.data.content;
        }
      );
    },
   
    btnShow(btn){
      let status = null;
      if (this.currentRow) {
        status = this.currentRow.CHDSTATUS;
      }
      switch (btn) {
          case '导出':
            return true;
            break;
          case '取消结账':
            return status === "B";
            break;
          case '取消挂账':
            return status === "D";
            break;
          case '作废':
            return status === "B";
            break;
          case '结账':
            return this.selectData.ChdstatusEquals == 'D' && status === 'D';
            break;
          default: return false;
        }
    },
    //作废
    chdInvalid() {
      this.$confirm("确认是否作废?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(callback => {
          let data = {
            CHDID: this.currentRow.CHDID
          };
          apiBase.DoService(
            data,
            apiBase.getApiAdress().Checkout.Invalid,
            res => {
              this.$message({
                type: "success",
                message: "作废成功!"
              });
              this.getData();
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    //业务收费查询个性化
    GetBusinesschargeFH(){
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Param.GetBusinesschargeFH,
        res => {
          this.istime = res.data.content;
        }
      );
    },
    //查询条件重置
    btnRepeat() {
      this.selectData = {
        ChdstatusEquals: null,
        RegbusnumEquals: null,
        ChddateGreaterAndEquals: null,
        ChddateLessAndEquals: null
      };
      this.selectData.ChddateGreaterAndEquals = new Date();
      this.selectData.ChddateLessAndEquals = new Date();
      if(!this.istoday){
      this.selectData.ChddateGreaterAndEquals.setDate(1);
      var day = new Date();
      var year = day.getFullYear();
      var month = day.getMonth()+1;
      var d = new Date(year, month, 0);
      var dat = d.getDate();
      this.selectData.ChddateLessAndEquals.setDate(dat);
      }
      this.getData();
    }
  },
  mounted() {
    this.selectData.ChdstatusEquals = this.all;
    // this.getData();
  },
  components: {
    dataTable,
    checkoutinfo,
    printmodal
  },

  created() {
    this.GetBusinesschargeFH();
    this.getColumns();
    // this.gettoday();
      let data = {};
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Param.GetIsBusinesschargeSearchDefaultToday,
        res => {
          this.istoday = res.data.content;
          this.selectData.ChddateGreaterAndEquals = new Date();
          this.selectData.ChddateLessAndEquals = new Date();
          if(!this.istoday){
            this.selectData.ChddateGreaterAndEquals.setDate(1);
            var day = new Date();
            var year = day.getFullYear();
            var month = day.getMonth()+1;
            var d = new Date(year, month, 0);
            var dat = d.getDate();
            this.selectData.ChddateLessAndEquals.setDate(dat);
          }
          this.getData()
        }
      );
    this.getChdStatus();
  }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
</style>

